import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class BannerPage extends StatefulWidget {
  //banner高度
  double _bannerHeight = 0.0;

  //banner数据
  List<Widget> _itemList;

  BannerPage(this._bannerHeight, this._itemList);

  @override
  State<StatefulWidget> createState() => _BannerPage();
}

class _BannerPage extends State<BannerPage> {
  PageController _pageController = PageController();
  int index = 0;
  int countDown = 3000;

  //计时器
  Timer _timer;

  @override
  void initState() {
    super.initState();
    // _makeData();
    _timer = Timer.periodic(Duration(milliseconds: countDown), (timer) {
      if (index < widget._itemList.length - 1) {
        index++;
      } else {
        index = 0;
      }
      _pageController.animateToPage(index,
          duration: Duration(milliseconds: 1500), curve: Curves.linear);
    });
  }

  @override
  void dispose() {
    _timer.cancel();
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: widget._bannerHeight,
      width: MediaQuery.of(context).size.width,
      child: PageView(
        controller: _pageController,
        children: widget._itemList,
      ),
    );
  }
}
